﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EpcTagLibrary.Pat
{
    public class SGTIN : Pattern
    {
        private int m_BitLength;

        public SGTIN(PatternField[] fields, Type type)
            : base(fields, type)
        {
        }

        public override int GetTagBitLength()
        {
            return m_BitLength;
        }

        public override string GetScheme()
        {
            return GetSchemeName() + "-" + GetTagBitLength();
        }

        public override string GetSchemeName()
        {
            return "sgtin";
        }

        public PatternField FilterComponent
        {
            get
            {
                return Fields[0];
            }
        }

        public PatternField CompanyPrefixComponent
        {
            get
            {
                return Fields[1];
            }
        }

        public PatternField ItemReferenceComponent
        {
            get
            {
                return Fields[2];
            }
        }

        public PatternField SerialNumberComponent
        {
            get
            {
                return Fields[3];
            }
        }

        public class Factory : PatternFactory
        {
            public override Pattern GetPattern(string patBody, bool isGroupingPattern, bool isPureid)
            {
                if (!patBody.StartsWith("sgtin"))
                {
                    throw new PatternParseException("Specified URI does not match the SGTIN URI for patterns.");
                }

                int Length = 198;

                if (!isPureid)
                {
                    Length = System.Convert.ToInt32(patBody.Substring(6, patBody.IndexOf(":")-6));
                }

                string[] Fields = patBody.Substring(patBody.IndexOf(":") + 1).Split(new char[1] { '.' });

                if (Fields.Length != 4)
                {
                    throw new PatternParseException("Specified URI does not match the SGTIN URI for patterns.");
                }

                PatternField[] PatFields = new PatternField[4];
                try
                {
                    PatFields[0] = new PatternField(Fields[0], isGroupingPattern, 3);
                    PatFields[0].Property = typeof(EpcTagLibrary.SGTIN).GetProperty("Filter");

                    PatFields[1] = new PaddedPatternField(Fields[1], isGroupingPattern, 40);
                    PatFields[1].Property = typeof(EpcTagLibrary.SGTIN).GetProperty("CompanyPrefix");

                    PatFields[2] = new PaddedPatternField(Fields[2], isGroupingPattern, 24);
                    PatFields[2].Property = typeof(EpcTagLibrary.SGTIN).GetProperty("ItemReference");

                    PatFields[3] = new PaddedPatternField(Fields[3], isGroupingPattern, Length == 96 ? 38 : 140);
                    PatFields[3].Property = typeof(EpcTagLibrary.SGTIN).GetProperty("SerialNumber");
                }
                catch (Exception e)
                {
                    throw new PatternParseException("Error occured setting up pattern component for a field: " + patBody, e);
                }

                SGTIN Result = new SGTIN(PatFields, typeof(EpcTagLibrary.SGTIN));
                Result.m_BitLength = Length;

                return Result;
            }
        }

    }
}
